
var RailingSprite = cc.Sprite.extend({
        disappearAction : null,
    tapping : false,
    tap_gap : {x:0, y:0},
    tap_start_pos : {x:0, y:0},
    debug : 1,
    onEnter : function() {
            this._super(); 

            this.disappearAction = this.createDisappearAction();
            this.disappearAction.retain(); 
    },

    onExit : function() {
            this.disappearAction.release();
            this._super();
    },

    //添加点击事件
    addTouchEventListense : function() {
            this.touchListener = cc.EventListener.create({
                    event : cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: false,
            onTouchBegan: this.onTouchBegan,
            onTouchMoved : this.onTouchMoved,
            onTouchEnded : this.onTouchEnded,
            });
            cc.eventManager.addListener(this.touchListener, this);
    },

    onTouchBegan : function (touch, event) {
            var pos = touch.getLocation();
            var target = event.getCurrentTarget();
            
            //console.log("touch began");
            

            if (cc.rectContainsPoint(target.getBoundingBox(), pos)) {
					
					target.setScale(1);
					
                    target.tap_start_pos.x = target.x;
                    target.tap_start_pos.y = target.y;

                    target.tap_gap.x = pos.x - target.getPositionX();
                    target.tap_gap.y = pos.y - target.getPositionY();

                    target.tapping = true;
                    target.getParent().updatetap();

                    target.getParent().railing_points.push(cc.p(target.x, target.y));
                    if (target.debug == 1){
                            target.getParent().dn.drawDot(cc.p(target.x, target.y), 5, cc.color(255,255,0));
                    }
                    return true;
            }
    },

    onTouchMoved : function (touch, event) {
            var target = event.getCurrentTarget();
            var pos = touch.getLocation();
            
            //console.log("touch moved");

            var new_x = pos.x - target.tap_gap.x;
            var new_y = pos.y - target.tap_gap.y;

            if (target.getParent().game_state != 1 || !target.tapping){
                    if (Math.sqrt((new_x - target.tap_start_pos.x)*(new_x - target.tap_start_pos.x) + (new_y - target.tap_start_pos.y) * (new_y - target.tap_start_pos.y)) > 20){
                            target.tapping = false;
                            target.getParent().updatetap();
                    }

                    return ;
            }

            target.x = pos.x - target.tap_gap.x;
            target.y = pos.y - target.tap_gap.y; 

            //检查是否可以触发交换
            var dx = target.x - target.tap_start_pos.x;
            var dy = target.y - target.tap_start_pos.y;

            if (Math.sqrt(dx*dx + dy*dy) >= /*target.width * target.scale*/target.getParent().horse_size / 2) {

                    var from = cc.p(target.tap_start_pos.x, target.tap_start_pos.y);
                    var to = cc.p(target.x, target.y);
                    target.getParent().dn.drawSegment(from, to, 2, cc.color(255,0,0));

                    target.tap_start_pos.x = target.x;
                    target.tap_start_pos.y = target.y;

                    target.getParent().railing_points.push(to);

                    if (target.debug == 1){
                            target.getParent().dn.drawDot(to, 5, cc.color(255,255,0));
                    }

                    //下面开始计算是否需要添加额外的点
                    var gap = target.getParent().horse_size;
                    var dis = Math.sqrt((from.x - to.x)*(from.x - to.x) + (from.y - to.y)*(from.y - to.y));
                    if (dis > gap) {
                            //需要插入点
                            var count = Math.ceil(dis / gap) - 1;
                            for (var i = 0; i < count ; i ++){
                                    var pp = cc.p(from.x + (to.x - from.x) * (i + 1) / (count + 1), from.y + (to.y - from.y) * (i + 1) / (count + 1)); 
                                    target.getParent().railing_points.push(pp); 
                                    if (target.debug == 1){
                                            target.getParent().dn.drawDot(pp, 5, cc.color(255,255,0));
                                    }
                            }
                    } 
                    //console.log("add line");
            } 
    },

    onTouchEnded : function (touch, event) {
            var target = event.getCurrentTarget();

            console.log("touch ended");
            target.setScale(0.5);

            if (target.tapping){
                    var from = cc.p(target.tap_start_pos.x, target.tap_start_pos.y);
                    var to = cc.p(target.x, target.y);
                    target.getParent().dn.drawSegment(from, to, 2, cc.color(255,0,0));
                    target.getParent().railing_points.push(to);
                    if (target.debug == 1){
                            target.getParent().dn.drawDot(to, 5, cc.color(255,255,0));
                    }
                    target.tapping = false;
                    target.getParent().updatetap();

                    if (target.getParent().game_state == 1){
                            cc.eventManager.removeListeners(target);
                    }
            }
    },

    runDisappear : function(target) { 
            console.log("runDisappear...");
            target.setZOrder(100);

            target.stopAllActions();
            //定义消失动画
            var ac = target.disappearAction;
            var seqAc = cc.Sequence.create(ac, cc.CallFunc.create(function() {
                    console.log("callfun....");

                    target.removeFromParent(); 
            }, target));

            target.runAction(seqAc);
    },

    createDisappearAction : function() { 
            var size = cc.winSize;
            var x = cc.random0To1() * size.width;
            var action = cc.MoveTo.create(0.1, cc.p(x, size.height + 30));
            return action;
    },

    removeTouchEventListenser : function(){
            cc.eventManager.removeListener(this.touchListener);
    },

});



